package com.atguigu.srb.core.controller.api;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserIndexVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Bright
 * @since 2022-04-09
 */
@Slf4j
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
public class UserInfoController {

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    UserInfoService userInfoService;

    @ApiOperation(value = "会员注册")
    @PostMapping("/register")
    public R register(
            @ApiParam(value = "会员对象", required = true)
            @RequestBody RegisterVO registerVO) {
        String code = registerVO.getCode();
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        //判断手机号是否为空
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //判断密码是否为空
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        //获取验证码
        String codeGen = redisTemplate.opsForValue().get("srb:sms:code" + mobile).toString();
        //判断验证码是否相等
        Assert.equals(code, codeGen, ResponseEnum.CODE_ERROR);

        userInfoService.register(registerVO);

        return R.ok().message("注册成功");


    }

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        UserInfoVO userInfoVO = userInfoService.login(loginVO, request.getRemoteAddr());

        return R.ok().message("登录成功").data("userInfo", userInfoVO);

    }

    @ApiOperation(value = "检查token")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (JwtUtils.checkToken(token)) {
            return R.ok().message("检测成功");
        } else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }

    }

    @ApiOperation(value = "校验手机号是否注册")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable String mobile) {
        return userInfoService.checkMobile(mobile);
    }

    @ApiOperation("获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public R getIndexUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        UserIndexVO userIndexVO = userInfoService.getIndexUserInfo(userId);
        return R.ok().data("userIndexVO", userIndexVO);
    }


}

